<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//引入模型
use App\Admin\Member;
use DB;
use Input;
class MemberController extends Controller
{
    //列表
    public function index(){
    	//获取数据
    	$data = Member::where('isdel','2')->get();
//  	var_dump($data);die;
    	//获取地区数据
//  	$countrys = DB::table('area') -> whereIn('id',$data['country_id']);
    	
    	
    	//展示视图
    	return view('admin.member.index',compact('data'));
    }

    //添加
    public function add(){
    	//判断请求类型
        if(Input::method() == 'POST'){
            //处理
            $data = Input::except('uploadfile','file-2','_token','file');
            //补充数据
            $data['password'] = bcrypt('password');
            $data['created_at'] = date('Y-m-d H:i:s');
            //写入数据
            $result = Member::insert($data);
            //转化bool类型
            return $result ? '1' : '0';
        }else{
            //查询所有的国家/地区数据
            $country = DB::table('area') -> where('pid','0') -> get();
            //展示视图
            return view('admin.member.add',compact('country'));
        }
    }

    //根据地区pid获取其下属内容
    public function getAreasByPid(){
    	//获取pid
    	$pid = Input::get('pid');
    	//查询
    	$data = DB::table('area') -> where('pid',$pid) -> get();
    	//编码处理
    	return response() -> json($data);
    }
    
    
    
   //禁用启用
    public function stat() {
//  	$id = $_POST['id']; 
    	$id = $_GET['id']; 
    	$info = Member::where('id','=',$id)->update(['status'=>1]);   	
    }
    
    public function stat2() {
    	$id = $_GET['id']; 
//  	$id = $_POST['id']; 
    	$info = Member::where('id','=',$id)->update(['status'=>2]);   	
    }
    
    //假删除用户（其实是把isdel字段改成1）
    public function del() {
    	$id = $_GET['id']; 
//  	$id = $_POST['id']; 
    	$info = DB::table('member')->where('id','=',$id)->update(['isdel'=>1]);
    }
    

    //修改用户信息
    public function edit() {
    	//判断
    	if(Input::method() == 'POST') {
    		
    		
    		$data = Input::except('_token','file');
    		
//  		var_dump($data);die;
    		
//  		$data['gender'] = 4;
    		
    		$result = Member::where('id',$data['id'])->update($data);
    		
    		return $result ? '1' : '0';
    		
//  		var_dump($result);die;
//			return $data;
    		
    	} else {
    		
    		//获取数据
    		$id = $_GET['id'];
    		$data = Member::where('id',$id)->get();
    		
    		//查询所有的国家/地区数据
            $country = DB::table('area') -> where('pid','0') -> get();
            
            
            $pro_id = DB::table('member')->where('id',$id)->value('province_id');
            $province = DB::table('area') -> where('id',$pro_id)->get();

            $cit_id = DB::table('member')->where('id',$id)->value('city_id');           
            $city = DB::table('area') -> where('id',$cit_id)->get();
            
            $cou_id = DB::table('member')->where('id',$id)->value('county_id');
            
            $county = DB::table('area') -> where('id',$cou_id)->get();
            
    		
    		//展示视图
    		return view('/admin/member/edit',compact('data','country','pro_id','province','cit_id','city','cou_id','county'));
    		
    	}
    	
    	
    }
    
    //批量删除(非ajax方法)
	/*public function  delMore(){
	    $id = input::get('id');
	    $str = explode(",",$id);
	    foreach($str as $v){
	        DB::table('admin')->where('id',"=","$v")->update(['isdel'=>'1']);
	    }
	    return  redirect("/admin/admin/index");
	}*/

    //批量删除(ajax方法)
	public function  delMore(){
	    $id = input::get('id');
	    $str = explode(",",$id);
	    foreach($str as $v){
	        DB::table('member')->where('id',"=","$v")->update(['isdel'=>'1']);
	    }
	    	    
	} 
	
	//按注册日期范围和用户名关键字模糊查询管理员信息
	public function chaxun() {
		
		//非ajax传值
		$username = $_GET['username'];
		$tstart = $_GET['tstart'];
		$tend = $_GET['tend'];
		
		if(empty($username) && empty($tstart) && empty($tend)){
			
			$data = Member::where('isdel','2')->get();
    		return view('/admin/member/index',compact('data'));
    		
		}elseif(!empty($username) && empty($tstart) && empty($tend)) {
			
			$data = Member::where('isdel','2')->where('username',$username)->get();
    		return view('/admin/member/index',compact('data'));
    		
		}elseif(empty($username) && !empty($tstart) && !empty($tend)) {
			
			$data = Member::where('isdel','2')->where('created_at','>=',$tstart)->where('created_at','<=',$tend)->get();
    		return view('/admin/member/index',compact('data'));
    		
		}elseif(!empty($username) && !empty($tstart) && !empty($tend)) {
			
			$data = Member::where('isdel','2')->where('created_at','>=',$tstart)->where('created_at','<=',$tend)->where('username','=',$username)->get();
    		return view('/admin/member/index',compact('data'));
    		
		}else{
			
			$data = Member::where('isdel','2')->get();
			return view('/admin/member/index',compact('data'));
		}		
		
	}
	
	
	//获取具体住址信息
	public function getArea(){
		
		$country_id = $_GET['country_id'];
		$province_id = $_GET['province_id'];
		$city_id = $_GET['city_id'];
		$county_id = $_GET['county_id'];
		
		$info['country'] = DB::table('area')->where('id',$country_id)->value('area');
		$info['province'] = DB::table('area')->where('id',$province_id)->value('area');
		$info['city'] = DB::table('area')->where('id',$city_id)->value('area');
		$info['county'] = DB::table('area')->where('id',$county_id)->value('area');
		
		return $info;
		
		
	}
	
	//删除的会员列表展示
	public function delList(){
		
		$data = Member::where('isdel',1)->get();
		return view('admin.member.delList',compact('data'));
		
		
	}
    
    //彻底删除用户
    public function del2() {
    	$id = $_GET['id']; 
    	$info = DB::table('member')->where('id','=',$id)->delete();
    	return $info;
    }


    //删除用户的还原
    public function huanyuan() {
    	$id = $_GET['id']; 
    	$info = DB::table('member')->where('id','=',$id)->update(['isdel'=>'2']);
    	return $info;
    }
    

    //批量删除(ajax方法)
	public function  delMore2(){
	    $id = input::get('id');
	    $str = explode(",",$id);
	    foreach($str as $v){
	        DB::table('member')->where('id',"=","$v")->delete();
	    }
	    	    
	} 
	
	
	//按注册日期范围和用户名关键字模糊查询管理员信息
	public function chaxun2() {
		
		//非ajax传值
		$username = $_GET['username'];
		$tstart = $_GET['tstart'];
		$tend = $_GET['tend'];
		
		if(empty($username) && empty($tstart) && empty($tend)){
			
			$data = Member::where('isdel','1')->get();
    		return view('/admin/member/delList',compact('data'));
    		
		}elseif(!empty($username) && empty($tstart) && empty($tend)) {
			
			$data = Member::where('isdel','1')->where('username',$username)->get();
    		return view('/admin/member/delList',compact('data'));
    		
		}elseif(empty($username) && !empty($tstart) && !empty($tend)) {
			
			$data = Member::where('isdel','1')->where('created_at','>',$tstart-1)->where('created_at','<',$tend+1)->get();
    		return view('/admin/member/delList',compact('data'));
    		
		}elseif(!empty($username) && !empty($tstart) && !empty($tend)) {
			
			$data = Member::where('isdel','1')->where('created_at','>',$tstart-1)->where('created_at','<',$tend+1)->where('username','=',$username)->get();
    		return view('/admin/member/delList',compact('data'));
    		
		}else{
			
			$data = Member::where('isdel','1')->get();
			return view('/admin/member/delList',compact('data'));
		}		
		
	}
	
	
	//用户等级管理展示
	public function dengji() {
		
		$data1 = Member::where('role_id','3')->get();
		$data2 = Member::where('role_id','4')->get();
		return view('admin.member.dengji',compact('data1','data2'));
		
	} 
	
	
	//用户等级改变为高级会员
	public function mRight() {
		
		$id = input::get('id');
	    $str = explode(",",$id);
	    foreach($str as $v){
	        DB::table('member')->where('id',"=","$v")->update(['role_id'=>'3']);
	    }
		
	}
	
	
	//用户等级改变为普通用户
	public function mLeft() {
		$id = input::get('id');
	    $str = explode(",",$id);
	    foreach($str as $v){
	        DB::table('member')->where('id',"=","$v")->update(['role_id'=>'4']);
	    }
		
		
	}
	
	
  
    
}
